﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
using HyperVSnapin.Object.Enums;
using HyperVSnapin.Helpers;


namespace HyperVSnapin.Object
{
    public class VirtualMachine : HyperVObject
    {
        private Host host;
        private const string MethodeRequestState = "RequestStateChange";

        protected internal VirtualMachine(ManagementObject mo, Host host)
            : base(mo)
        {
            this.host = host;
        }
        /// <summary>
        /// A user-friendly name for the object
        /// </summary>
        /// <remarks>Set to the display name of the computer for a virtual system or the NetBIOS name of the host system</remarks>
        public override string Name
        {
            get
            {
                return ManagmenetObject.Properties["ElementName"].Value.ToString();
            }
        }

        /// <summary>
        /// Indicates the enabled and disabled states of an element
        /// </summary>
        /// <remarks>
        /// It can also indicate the transitions between these requested states.
        /// Set to 2 ("Enabled") for a host system or one of the following values for a virtual system
        /// </remarks>
        public EnabledStateEnum EnabledState
        {
            get
            {
                return (EnabledStateEnum)Enum.Parse(typeof(EnabledStateEnum), ManagmenetObject.Properties["EnabledState"].Value.ToString());
            }
        }

        protected internal Host Host
        {
            get
            {
                return this.host;
            }
        }

        /// <summary>
        /// Allow to change state of VM
        /// </summary>
        /// <remarks>Requests that the state of the computer system be changed. 
        /// This method is only valid for instances of Msvm_ComputerSystem which represent a virtual computer system. 
        /// Valid values for the RequestedState input parameter are: 0 ("Unknown"), 2 ("Enabled"), 3 (Disabled"), 
        /// 32768 ("Paused"), and 32769 ("Suspended").</remarks>
        protected internal void RequestStateChange(EnabledStateEnum newState)
        {
            if (newState != EnabledStateEnum.Unknown && newState != EnabledStateEnum.Enabled && newState != EnabledStateEnum.Disabled
                && newState != EnabledStateEnum.Paused && newState != EnabledStateEnum.Suspended)
            {
                throw new InvalidOperationException("Invalid requested State");
            }

            VirtualMachineWMIHelper helper = new VirtualMachineWMIHelper();
            helper.RequestStateChange(this.ManagmenetObject, (int)newState);            
        }
    }
}
